/**
 * 点菜
 *@Description
 *@author your name
 *@createDate 2011-6-7 下午09:27:35
 *@version V1.0
 */
package com.welldone.service.orderdishes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.welldone.service.DBService;
import com.welldone.util.CommonUtil;

public class OrderDishes {

	/**
	 * 点菜
	 *@author your name
	 *@createDate 2011-6-10
	 *@return
	 */
	public boolean orderDishes(Map insertMap){
		boolean flag = false;
		 Map m = insertMap;
		 List list =(List) m.get("more");
		String[] sqlArrayList = new String[list.size()+1];
		String mainMenuId = UUID.randomUUID().toString();
		String subMenuId = UUID.randomUUID().toString();
		String sqlMain="insert into t_menu_main (menuId,tableId,isPrint,menuStatus) " +
		"values ('"+mainMenuId+"','"+m.get("tableId")+"','0','0')";
		sqlArrayList[0]=sqlMain;
		String sqlMore ="";
		for(int i=0;i<list.size();i++){
			Map getM = (Map)list.get(i);
			sqlMore= "insert into t_menu_more (ID,menuId,vegetablesId,copies,vegetablesStatus,memo) " +
					"values('"+subMenuId+"','"+mainMenuId+"','"+getM.get("vegetablesId")+"',"+getM.get("copies")+",'0',"+CommonUtil.escapeNullForSql(getM.get("memo"), false)+")";
		sqlArrayList[i+1]=sqlMore;
		}
		try{
			
			DBService.getJdbcTemplate().batchUpdate(sqlArrayList);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
		
	}
	
	/**
	 * 删除点菜单
	 *@author your name
	 *@createDate 2011-7-1
	 *@param menuId
	 *@return
	 */
	public boolean delDishes(String menuId){
		boolean flag = false;
		String[] sqlArray = {"delete from t_menu_main where menuId='"+menuId+"'","delete from t_menu_more where menuId='"+menuId+"'"};
		try{
			DBService.getJdbcTemplate().batchUpdate(sqlArray);
			flag = true;
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	} 
	
	/**
	 * 更新菜单
	 *@author your name
	 *@createDate 2011-7-4
	 *@param map
	 *@return
	 */
	public boolean updateDishes(Map map){
		boolean flag = false;
		int index = 0;
		List updateMoreList =(List) map.get("update");
		List updateDelList = (List) map.get("del");
		List updateInsertList = (List)map.get("insert");
		String [] sqlArray = new String[updateMoreList.size()+updateDelList.size()+updateInsertList.size()+1];
		String updateMainSql = "update t_menu_main set tableId = '"+map.get("tableId")+"' where menuId = '"+map.get("menuId")+"' ";
		String updateMoreSql="";
		String updateDelSql="";
		String updateInsertSql="";
		sqlArray[0] = updateMainSql;
		/**拼接要更新的SQL**/
		for(int i=0;i<updateMoreList.size();i++){
			Map m1 = (Map)updateMoreList.get(i);
			updateMoreSql = "update t_menu_more set vegetablesId='"+m1.get("vegetablesId")+"',copies="+m1.get("copies")+",memo='"+m1.get("memo")+"' where ID='"+m1.get("ID")+"'";
			sqlArray[i+1] = updateMoreSql;
			index++;
		}
		/**拼接要删除的SQL**/
		for(int i=0;i<updateDelList.size();i++){
			Map m2 = (Map)updateDelList.get(i);
			Map m = getMoreByMoreId(m2.get("ID").toString());
			if(null!=m){
				int copies =Integer.valueOf(m.get("copies").toString());
				int oldCopies = Integer.valueOf(m2.get("copies").toString());
				if((oldCopies-copies)==0){
					updateDelSql = "delete from t_menu_more where id='"+m2.get("ID")+"'";
				}else if ((copies<oldCopies) && copies>0){
					updateDelSql = "update t_menu_more set copies ="+copies+" where ID='"+m2.get("ID")+"'";
				}
			}
			index++;
			sqlArray[index] = updateDelSql;
			
		}
		/**拼接要增加的SQL**/
		for(int i=0;i<updateDelList.size();i++){
			Map m2 = (Map)updateDelList.get(i);
			Map m = getMoreByMoreId(m2.get("ID").toString());
			if(null!=m){
				int copies =Integer.valueOf(m.get("copies").toString());
				int oldCopies = Integer.valueOf(m2.get("copies").toString());
				if ((copies>oldCopies) && copies>0){
					updateInsertSql = "update t_menu_more set copies ="+copies+" where ID='"+m2.get("ID")+"'";
				}else if (copies ==-1){
					String mainMenuId = UUID.randomUUID().toString();
					String subMenuId = UUID.randomUUID().toString();
					updateInsertSql= "insert into t_menu_more (ID,menuId,vegetablesId,copies,vegetablesStatus,memo) " +
					"values('"+subMenuId+"','"+mainMenuId+"','"+m2.get("vegetablesId")+"',"+m2.get("copies")+",'0',"+CommonUtil.escapeNullForSql(m2.get("memo"), false)+")";
				}
			}
			index++;
			sqlArray[index] = updateDelSql;
		}
		
		try{
			DBService.getJdbcTemplate().batchUpdate(sqlArray);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();;
		}
		return flag;
	}
	
	/**
	 * 根据条件，或没有，查询点菜单
	 *@author your name
	 *@createDate 2011-7-4
	 *@param querymap
	 *@return
	 */
	public Map getOrderDishes(Map querymap){
		final Map retMap = new HashMap();
		final List mainList = new ArrayList();
		StringBuffer where  = new StringBuffer();
		String sqlMain = "select * from t_menu_main where 1=1 and "+where.toString();
		final String sqlMore = "";
		return (Map)DBService.getJdbcTemplate().query(sqlMain, new ResultSetExtractor() {
			
			@Override
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				 List moreList = new ArrayList();
				while(rs.next()){
					Map mm = new HashMap();
					String menuId = rs.getString("menuId");
					mm.put("menuId", menuId);
					mm.put("tableId", rs.getString("tableId"));
					mm.put("isPrint", rs.getString("isPrint"));
					mm.put("menuStatus", rs.getString("menuStatus"));
					mainList.add(mm);
					moreList = DBService.getJdbcTemplate().queryForList("select * from t_menu_more where menuId = '"+menuId+"'");
				}
				retMap.put("mainList", mainList);
				retMap.put("moreList", moreList);
				return retMap;
			}
		});
	}
	
	/**
	 * 根据ID查询附单
	 *@author your name
	 *@createDate 2011-7-4
	 *@param moreId
	 *@return
	 */
	public Map getMoreByMoreId(String moreId){
		String sql="select * from t_menu_more where id='"+moreId+"'";
		return (Map)DBService.getJdbcTemplate().query(sql, new ResultSetExtractor() {
			
			@Override
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				Map m =null;
				while(rs.next()){
					 m=new HashMap();
					m.put("ID", rs.getString("ID"));
					m.put("menuId", rs.getString("menuId"));
					m.put("vegetablesId", rs.getString("vegetablesId"));
					m.put("copies", rs.getInt("copies"));
					m.put("vegetablesStatus", rs.getString("vegetablesStatus"));
					m.put("memo", rs.getString("memo"));
				}
				return m;
			}
		});
	}
	/**
	 * 将是否打印设置为1，
	 *@author your name
	 *@createDate 2011-7-6
	 *@return
	 */
	public boolean updateIsPrint(){
		boolean flag = false;
		String sql="update t_menu_main set isPrint = '1'";
		int result = DBService.getJdbcTemplate().update(sql);
		if(result!=-1){
			flag = true;
		}
		return flag;
	}
	
	
}


